package com.jsyxdl.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.pdm.lifestage.model.dto.EntityLifeCycleDTO;
import com.hustcad.plm.pdm.lifestage.model.dto.LifeCycleDTO;
import com.hustcad.plm.pdm.lifestage.model.dto.ObjLifeStateDTO;
import com.hustcad.plm.pdm.lifestage.service.TyplmLifecycleTemplateService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.service.impl.extension.TyplmDefaultBomConvertExtensionServiceImpl;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.jsyxdl.utils.LifeCycleOperatorListenerUtils;
import com.ty.basic.dto.ObjLinkCopyDTO;
import com.ty.basic.entity.IdentifierEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Service
@Primary
public class ExtendTyplmDefaultBomConvertExtensionServiceImpl extends TyplmDefaultBomConvertExtensionServiceImpl {
    @Resource
    public TyplmPartService typlmPartService;
    @Autowired
    private TyplmLifecycleTemplateService typlmLifecycleTemplateService;


    @Override
    public void handleAfterCreateDownStream(List<ObjLinkCopyDTO> partObjLinkCopyDTOList, List<ObjLinkCopyDTO> bomObjLinkCopyDTOList) {

        List<EntityLifeCycleDTO> entityLifeCycleList = new ArrayList();

        for(int i=0;i<partObjLinkCopyDTOList.size();i++){
            BigInteger oid = partObjLinkCopyDTOList.get(i).getTargetOid();
            String oType = "ty.inteplm.part.CTyPart";
            PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(oid, oType);

            if("标准件".equals(partDetailsVO.getTypeName())||"辅料".equals(partDetailsVO.getTypeName())){
                IdentifierEntity identifierEntity=new IdentifierEntity();
                identifierEntity.setOid(new BigInteger(partDetailsVO.getOid()));
                identifierEntity.setOtype(partDetailsVO.getOtype());
                List<IdentifierEntity> identifierEntities=new ArrayList<>();
                identifierEntities.add(identifierEntity);
                List<ObjLifeStateDTO> lifeList=this.typlmLifecycleTemplateService.batchQueryEntityLifecycleState(identifierEntities);
                if(lifeList.size()>0){
                    boolean isAdd=false;
                    if(isAdd==false){
                        for(int a=0;a<lifeList.get(0).getLifecycleList().size();a++){
                            if(lifeList.get(0).getLifecycleList().get(a).getName().indexOf("设计发布")!= -1){
                                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                                entityLifeCycleDTO.setOid(new BigInteger(partDetailsVO.getOid()));
                                entityLifeCycleDTO.setOtype(partDetailsVO.getOtype());
                                entityLifeCycleDTO.setLifeCycleStageKey("DESIGN APPROVED");
                                entityLifeCycleList.add(entityLifeCycleDTO);
                                isAdd=true;
                            }
                        }
                    }
                    if(isAdd==false){
                        for(int a=0;a<lifeList.get(0).getLifecycleList().size();a++){
                            if(lifeList.get(0).getLifecycleList().get(a).getName().indexOf("样机发布")!= -1){
                                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                                entityLifeCycleDTO.setOid(new BigInteger(partDetailsVO.getOid()));
                                entityLifeCycleDTO.setOtype(partDetailsVO.getOtype());
                                entityLifeCycleDTO.setLifeCycleStageKey("PROTOTYPE");
                                entityLifeCycleList.add(entityLifeCycleDTO);
                                isAdd=true;
                            }
                        }
                    }
                    if(isAdd==false){
                        for(int a=0;a<lifeList.get(0).getLifecycleList().size();a++){
                            if(lifeList.get(0).getLifecycleList().get(a).getName().indexOf("小批量发布")!= -1){
                                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                                entityLifeCycleDTO.setOid(new BigInteger(partDetailsVO.getOid()));
                                entityLifeCycleDTO.setOtype(partDetailsVO.getOtype());
                                entityLifeCycleDTO.setLifeCycleStageKey("TRIALPRODUCTION");
                                entityLifeCycleList.add(entityLifeCycleDTO);
                                isAdd=true;
                            }
                        }
                    }
                    if(isAdd==false){
                        for(int a=0;a<lifeList.get(0).getLifecycleList().size();a++){
                            if(lifeList.get(0).getLifecycleList().get(a).getName().indexOf("量产发布")!= -1){
                                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                                entityLifeCycleDTO.setOid(new BigInteger(partDetailsVO.getOid()));
                                entityLifeCycleDTO.setOtype(partDetailsVO.getOtype());
                                entityLifeCycleDTO.setLifeCycleStageKey("PRODUCTION");
                                entityLifeCycleList.add(entityLifeCycleDTO);
                                isAdd=true;
                            }
                        }
                    }
                    if(isAdd==false){
                        for(int a=0;a<lifeList.get(0).getLifecycleList().size();a++){
                            if(lifeList.get(0).getLifecycleList().get(a).getName().indexOf("生产已发布")!= -1){
                                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                                entityLifeCycleDTO.setOid(new BigInteger(partDetailsVO.getOid()));
                                entityLifeCycleDTO.setOtype(partDetailsVO.getOtype());
                                entityLifeCycleDTO.setLifeCycleStageKey("PRODUCTION_RELEASED");
                                entityLifeCycleList.add(entityLifeCycleDTO);
                                isAdd=true;
                            }
                        }
                    }
                    if(isAdd==false){
                        for(int a=0;a<lifeList.get(0).getLifecycleList().size();a++){
                            if(lifeList.get(0).getLifecycleList().get(a).getName().indexOf("已发布")!= -1){
                                EntityLifeCycleDTO entityLifeCycleDTO = new EntityLifeCycleDTO();
                                entityLifeCycleDTO.setOid(new BigInteger(partDetailsVO.getOid()));
                                entityLifeCycleDTO.setOtype(partDetailsVO.getOtype());
                                entityLifeCycleDTO.setLifeCycleStageKey("RELEASED");
                                entityLifeCycleList.add(entityLifeCycleDTO);
                                isAdd=true;
                            }
                        }
                    }
                }
            }
        }

        if (entityLifeCycleList.size() > 0) {
            LifeCycleDTO lifeCycleDTO = new LifeCycleDTO();
            lifeCycleDTO.setEntityLifeCycleDTOS(entityLifeCycleList);
            lifeCycleDTO.setExcludeException(false);

            List<EntityLifeCycleDTO> entityLifeCycleDTOS = lifeCycleDTO.getEntityLifeCycleDTOS();
            LifeCycleOperatorListenerUtils lifeCycleOperatorListenerUtils=new LifeCycleOperatorListenerUtils();
            lifeCycleOperatorListenerUtils.batchUpdateEntityLifecycle(entityLifeCycleDTOS, false,false, false);
        }
    }
}
